﻿using Infrastructure.FileFunction.Operation;
using System;
using System.IO;
using System.Xml.Serialization;

namespace Infrastructure.FileFunction.Serialize
{
    /// <summary>  
    /// <remarks>Xml序列化与反序列化</remarks>  
    /// <creator>zhangdapeng</creator>  
    /// </summary>
    public static class XmlSerializeUtil
    {
        #region 反序列化  
        /// <summary>  
        /// 反序列化  
        /// </summary>  
        /// <param name="xmlStr">XML字符串</param>  
        /// <returns></returns>  
        public static T DeserializeXmlStr<T>(string xmlStr) where T : class
        {
            if (string.IsNullOrWhiteSpace(xmlStr))
            {
                throw new FormatException("字符串为空");
            }
            using (StringReader sr = new StringReader(xmlStr))
            {
                XmlSerializer xmldes = new XmlSerializer(typeof(T));
                return xmldes.Deserialize(sr) as T;
            }
        }

        /// <summary>
        /// 反序列化XML为类实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath">路径</param>
        /// <returns></returns>
        public static T DeserializeXml<T>(string filePath) where T : class
        {
            if (!FilesUtils.FileExistes(filePath))
            {
                throw new FileNotFoundException($"{filePath}不存在");
            }
            using (var fs = new FileStream(filePath, FileMode.Open))
            {
                var serializer = new XmlSerializer(typeof(T));
                var sr = new StreamReader(fs, System.Text.Encoding.UTF8);
                return (T)serializer.Deserialize(sr);
            }
        }

        /// <summary>
        /// 反序列化XML为类实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath">路径</param>
        /// <returns></returns>
        public static T DeserializeXmlOrDefault<T>(string filePath) where T : class
        {
            if (!FilesUtils.FileExistes(filePath))
            {
                return default(T);
            }
            using (var fs = new FileStream(filePath, FileMode.Open))
            {
                var serializer = new XmlSerializer(typeof(T));
                var sr = new StreamReader(fs, System.Text.Encoding.UTF8);
                return (T)serializer.Deserialize(sr);
            }
        }
        #endregion

        #region 序列化  
        /// <summary>
        /// 序列化
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string SerializerToXmlStr<T>(this T obj) where T : class
        {
            using (MemoryStream stream = new MemoryStream())
            {
                XmlSerializer xml = new XmlSerializer(typeof(T));
                //序列化对象  
                xml.Serialize(stream, obj);
                stream.Position = 0;
                StreamReader sr = new StreamReader(stream);
                return sr.ReadToEnd();
                //sr.Dispose();
                //stream.Dispose();
            }
        }

        /// <summary>
        /// 序列化类实例为XML文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="dirPath">路径</param>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static string SerializeXml<T>(this T obj, string dirPath, string fileName) where T : class
        {
            FilesUtils.CreateDir(dirPath);

            var filePath = $"{dirPath}\\{fileName}";

            using (TextWriter writer = new StreamWriter(filePath))
            {
                new XmlSerializer(obj.GetType()).Serialize(writer, obj);
                return writer.ToString();
            }
        }
        #endregion
    }
}
